BUILD_DIR=.build
EXECUTABLES=KrautWiimote.exe KrautKeymote.exe KrautConnectWiimote.exe
NXTCOMM_DIR=lib/nxtcomm-2.0
WIIYOURSELF_DIR=lib/WiiYourself
export CXXFLAGS=-g -I$(NXTCOMM_DIR) -Ilib -I../KrautMobil

help:
	@echo KrautRemote makefile. These are the available targets:
	@echo "   all       - builds the remote executables + all dependencies"
	@echo "   install   - moves the executables to the dist/ folder, ready to commit"
	@echo "   libraries - builds the nxtcomm and WiiYourself libraries"
	@echo "   help      - prints this help message"
	@echo "   clean     - cleans all build products"

all: libraries $(EXECUTABLES)

.PHONY: help clean all install libraries

install: $(EXECUTABLES)
	mkdir -p dist
	mv $(EXECUTABLES) dist/

$(BUILD_DIR):
	mkdir -p $@

KrautWiimote.exe: KrautWiimote.o $(WIIYOURSELF_DIR)/wiimote.o $(NXTCOMM_DIR)/nxtcomm.a $(BUILD_DIR)
	g++ KrautWiimote.o $(WIIYOURSELF_DIR)/wiimote.o -lwinmm -lhid -lsetupapi $(NXTCOMM_DIR)/nxtcomm.a -static -o $@

KrautConnectWiimote.exe: KrautConnectWiimote.o $(WIIYOURSELF_DIR)/wiimote.o $(NXTCOMM_DIR)/nxtcomm.a $(BUILD_DIR)
	g++ KrautConnectWiimote.o $(WIIYOURSELF_DIR)/wiimote.o -lwinmm -lhid -lsetupapi $(NXTCOMM_DIR)/nxtcomm.a -static -o $@

KrautKeymote.exe: KrautKeymote.o $(NXTCOMM_DIR)/nxtcomm.a $(BUILD_DIR)
	g++ KrautKeymote.o $(NXTCOMM_DIR)/nxtcomm.a -static -o $@
	
$(WIIYOURSELF_DIR)/wiimote.o:
	@$(MAKE) -C $(WIIYOURSELF_DIR) all

$(NXTCOMM_DIR)/nxtcomm.a:
	@$(MAKE) -C $(NXTCOMM_DIR)     all

libraries: $(WIIYOURSELF_DIR)/wiimote.o $(NXTCOMM_DIR)/nxtcomm.a

clean:
	$(MAKE) -C $(NXTCOMM_DIR) clean
	-rm -rf $(WIIYOURSELF_DIR)/*.o
	-rm -rf *.o *.exe
	-rm -rf $(EXECUTABLES)
	-rmdir -rf $(BUILD_DIR)
